// pages/category/category.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categoryList: [],
    activeIndex: 0,
    categoryAd: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let url = 'https://ms.viomi.com.cn/vstore-api/layout/open-api/v1/category/top/list.json?source=1'
    wx.request({
      url: url,
      success: (res) => {
        this.setData({
          categoryList: res.data.mobBaseRes.result.categoryList
        })
      }
    })
    this._fetchAd()
  },

  // 点击切换选中分类
   handleCategory (e) {
    //  判断重复点击不做处理
     let index = Number(e.currentTarget.id)
     if(index === this.data.activeIndex) return
     this.setData({
       activeIndex: index
     })
   },
  //  广告
   _fetchAd() {
    wx.request({
      url: 'https://ms.viomi.com.cn/ad-web/services/ad/admin/categoryAd/listByCodeAndChannel?code=5004&channel=4',
      success: (res) => {
        this.setData({
          categoryAd: res.data.mobBaseRes.result.list
        })
      }
    })
  }
})